package com.wanmait.CarTrading.webController;

import com.wanmait.CarTrading.DAO.UserinfoDAO;
import com.wanmait.CarTrading.DAO.impl.UserinfoDAOImpl;
import com.wanmait.CarTrading.Util.CookieUtils;
import com.wanmait.CarTrading.Util.ImageCodeUtil;
import com.wanmait.CarTrading.Util.MD5Utils;
import com.wanmait.CarTrading.vo.Userinfo;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class CommentWebController
{
    @Resource
    private UserinfoDAO userinfoDAO;
    @RequestMapping({"/","/index"})
    public String index()
    {
        return "web/index";
    }
    @GetMapping("web/login")
    public void login(){
        //请求头：浏览器告诉服务器关于浏览器的一些信息
        //响应头：服务器告诉浏览器关于服务器的一些信息
        //获得所有请求头的名
        /*Enumeration<String> headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements())
        {
            String headerName = headerNames.nextElement();
            System.out.println(headerName+"="+request.getHeader(headerName));
        }*/
        //来路
        //String referer = request.getHeader("referer");

    }
    @PostMapping("/login")
    public String login(Userinfo userinfo, String referer, String code, String rememberMe, HttpServletRequest request, HttpServletResponse response)
    {
        //检查验证码
        String rightCode = (String)request.getSession().getAttribute("code");
        if(rightCode==null || !rightCode.equalsIgnoreCase(code))
        {

            return "web/login?msg=code&referer="+referer;
        }
        if(StringUtils.isEmpty(userinfo.getUsername()) || StringUtils.isEmpty(userinfo.getPassword()))
        {

            return "web/login?msg=error&referer="+referer;
        }

        Userinfo loginUserinfo =userinfoDAO.findByUsername(userinfo.getUsername());


        if(loginUserinfo==null)
        {

            return "web/login?msg=error&referer="+referer;
        }
        else
        {

            String loginPassword = loginUserinfo.getPassword();

            String password = MD5Utils.mymd5(userinfo.getPassword(),loginUserinfo.getSalt());

            if(!password.equals(loginPassword))
            {

                return "web/login?msg=error&referer="+referer;
            }
            else {

                request.getSession().setAttribute("userinfo",loginUserinfo);
                //检查是否选中了记住我
                if(rememberMe!=null)
                {

                    CookieUtils.addCookie("username",userinfo.getUsername(),"/",24*60*60*30,response);
                    CookieUtils.addCookie("password",loginUserinfo.getPassword(),"/",24*60*60*30,response);

                }
                else
                {

                    //删除cookie 退出时和修改密码时都需要删除cookie
                    CookieUtils.addCookie("username","","/",0,response);
                    CookieUtils.addCookie("password","","/",0,response);

                }

                if(!StringUtils.isEmpty(referer) && !referer.contains("/login")&&!referer.contains("/reg"))
                {
                    return "redirect:"+referer;
                }
                else {
                    return "web/index";
                }
            }
        }

    }

    @GetMapping("/reg")
    public void reg(){

    }
    @RequestMapping("/code")
    @ResponseBody
    public void code(HttpServletRequest request,HttpServletResponse response)
    {
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);


        ImageCodeUtil vCode = new ImageCodeUtil(120,40,5,30);
        request.getSession().setAttribute("code", vCode.getCode());
        System.out.println(vCode.getCode()+"ertyuiop[dfyui");
        try {
            vCode.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("checkCode")
    @ResponseBody
    public String checkCode(String code, HttpServletRequest request)
    {
        String rightCode = (String)request.getSession().getAttribute("code");
        if(rightCode!=null && rightCode.equalsIgnoreCase(code))
        {
            return "ok";
        }
        else
        {
            return "error";
        }
    }

}
